{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "<generator object <genexpr> at 0x000001F66FEE1EE0>\n",
      "True\n",
      "0\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "#可迭代对象 Iterable\n",
    "#数据类型有  list tuple string set dict等\n",
    "#可迭代对象的条件：\n",
    "#1.实现了__iter__方法 返回可迭代对象\n",
    "#2.实现了__next__方法 获取下一个值，并复制给临时变量i\n",
    "# 使用isinstance()判断是否是可迭代对象\n",
    "from collections.abc import Iterable\n",
    "#isinstance(o,t)判断是否是可迭代对象 返回bool类型 o:对象 t:类型 \n",
    "print(isinstance([],Iterable)) #True \n",
    "#可迭代器 Iterator 和可迭代对象的区别  可迭代对象是实现了__iter__方法的对象  可迭代器是实现了__iter__和__next__方法的对象\n",
    "#1. 迭代器 一定是可迭代对象 \n",
    "\n",
    "# class MyIterator:\n",
    "#     def __init__(self):\n",
    "#         self.data = 0\n",
    "#     def __iter__(self):\n",
    "#         return self #返回可迭代对象\n",
    "#     def __next__(self):\n",
    "#         if self.data > 10:\n",
    "#             raise StopIteration(\"迭代结束,没有更多数据了\")\n",
    "#         else:\n",
    "#             self.data += 1\n",
    "#             return self.data\n",
    "# myiter = MyIterator()\n",
    "# print(isinstance(myiter,Iterable)) #True\n",
    "# for i in myiter:\n",
    "#     print(i) #1 2 3 4 5 6 7 8 9 10\n",
    "#列表推导器\n",
    "# li=[i*5 for i in range(10)] #列表推导器\n",
    "# print(li) #[0, 5, 10, 15, 20, 25, 30, 35, 40, 45]\n",
    "# # for i in range(10): 和上面一样\n",
    "# #     print(i*5)\n",
    "\n",
    "gen=(i*5 for i in range(10)) #生成器表达式\n",
    "print(gen) #<generator object <genexpr> at 0x0000020999999990>\n",
    "print(isinstance(gen,Iterable)) #True\n",
    "print(next(gen))#0\n",
    "print(next(gen))#5\n",
    "#生成器函数 yeild 关键字\n",
    "#类似return 但是会暂停函数执行，下次调用时从暂停的地方继续执行\n",
    "#可迭代对象>迭代器>生成器\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
